package com.stream.demo;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * <p>
 * stream基础案例
 * </p>
 *
 * @author gzj
 * @since 2022/10/11
 */
@Component
@Configurable
public class PubSubDemo {

    //Supplier声明一个消息生产者，source就对应source-out-0的生产者binding
    @Bean
    public Supplier<String> source() {
        return () -> {
            String message = "from source";
            System.out.println(message);
            return message;
        };
    }

    //Function方式声明binding，相当于同时声明了一个Producer的Bindng和一个Consumer的Binding。transfer-in-0 表示消费者  transfer-out-0 表示生产者
    //其作用就在于，echo-in-0收到的消息，立即就会通过echo-out-0发送出去。
    @Bean
    public Function<String, String> tranform() {
        return message -> {
            System.out.println("tranform->" + message);
            return "tranform:" + message;
        };
    }

    //Consumer声明一个消息消费者，sink1就对应sink-in-0
    @Bean
    public Consumer<String> sink() {
        return message -> {
            System.out.println("sink->" + message);
        };
    }
}
